package com.oreilly.ez.base;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;

@Aspect
//@Component
@Order(2)
public class AuthorizedAspect {


    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void requestMapping(){

    }


    @Pointcut("execution(* com.oreilly.ez.web.*(..))")
    public void methodPointCut(){}



    @Before("requestMapping() && methodPointCut() && @annotation(authorized)")
    public void doBefore(JoinPoint joinPoint, Authorized authorized) throws Exception {

        Class type = joinPoint.getSignature().getDeclaringType();
        Annotation[] annotations = type.getAnnotationsByType(Authorized.class);
        if(annotations != null && annotations.length >0){
            System.out.println("直接类认证");
            return;
        }


        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        String token = request.getHeader("token");

    }




}
